<template>
  <view class="login">
    <view class="login-title">欢迎使用文鳐系统</view>
    <view class="login-content">为中小企业提供便捷服务，提升企业影响力</view>
    <img src="../../static/login/picture.png" style="width: 228px; height: 150px"/>
    <u--form ref="form" label-position="left" :model="temp" class="login-form">
      <u-form-item prop="username" class="login-item1">
        <u--input v-model="temp.username" placeholder="请输入用户名" clearable prefix-icon="account" border="none"/>
      </u-form-item>
      <u-form-item prop="password">
        <u-input
          v-model="temp.password" placeholder="请输入密码" border="none" clearable prefix-icon="lock"
          :password="password">
          <template #suffix>
            <u-icon :name="password?'eye-off':'eye-fill'" color=" #909399" size="18px" @click="lookOver"/>
          </template>
        </u-input>
      </u-form-item>
      <u-button text="登录" shape="circle" type="primary" class="submitbtn" @click="submit"/>
    </u--form>
    <u-toast ref="uToast"/>
  </view>
</template>

<script>
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      password: true,
      temp: {
        username: process.env.NODE_ENV === 'development' ? 'admin' : undefined,
        password: process.env.NODE_ENV === 'development' ? '123456' : undefined
      },
      rules: {
        username: [{
          required: true,
          message: '请填写用户名'
        }],
        password: [{
          required: true,
          message: '请填写密码'
        }, {
          asyncValidator: validatePassword
        }]
      }
    }
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  methods: {
    submit() {
      this.$refs.form.validate().then(() => {
        this.$store.userStore.login(this.temp).then((response) => {
          if (response.data.success) {
            this.$refs.uToast.show({
              type: 'success',
              duration: '1000',
              message: '登陆成功',
              complete: () => {
                uni.switchTab({
                  url: '/pages/index/index'
                })
              }
            })
          } else {
            this.$refs.uToast.show({
              type: 'error',
              duration: '1000',
              message: response.data.msg
            })
          }
        })
      })
    },
    lookOver() {
      this.password = !this.password
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  position: relative;
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background: linear-gradient(180deg, #6795f7 0%, #284cd8 100%);
  text-align: center;
  z-index: 0;

  &-title {
    padding-top: 50px;
    font-size: 22px;
    font-weight: bold;
    color: #ffffff;
    letter-spacing: 5px;
  }

  &-content {
    margin-top: 40rpx;
    margin-bottom: 40rpx;
    font-size: 14px;
    font-weight: 500;
    color: #ffffff;
    letter-spacing: 3px;
  }

  &-form {
    background: #ffffff;
    border-radius: 10px;
    padding: 20px;
    margin: -7vh 6vw;

    .login-item1 {
      margin-top: 30px;
    }

    .submitbtn {
      margin-top: 20px;
    }
  }
}
</style>
